﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace ControlLibrary
{
    /// <summary>
    /// ImageButton
    /// </summary>
    public partial class ImageButton : Control
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public ImageButton()
        {
        }

        private Image m_imgBackground = null;
        private bool m_bPressed = false;

        /// <summary>
        /// Property for the background image to be drawn behind the button text.
        /// </summary>
        public override Image BackgroundImage
        {
            get
            {
                return this.m_imgBackground;
            }
            set
            {
                this.m_imgBackground = value;
            }
        }

        // When the mouse button is pressed, set the "pressed" flag to true 
        // and invalidate the form to cause a repaint.  The .NET Compact Framework 
        // sets the mouse capture automatically.
        /// <summary>
        /// 鼠标按下
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            this.m_bPressed = true;
            this.Invalidate();
            base.OnMouseDown(e);
        }

        // When the mouse is released, reset the "pressed" flag 
        // and invalidate to redraw the button in the unpressed state.
        /// <summary>
        /// 鼠标按键弹起
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            this.m_bPressed = false;
            this.Invalidate();
            base.OnMouseUp(e);
        }

        // Override the OnPaint method to draw the background image and the text.
        /// <summary>
        /// 画图
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(new SolidBrush(SystemColors.ActiveCaption), e.ClipRectangle);

            if (this.m_imgBackground != null)
            {
                ImageAttributes attr = new ImageAttributes();
                attr.SetColorKey(Color.Magenta, Color.Magenta);

                if (this.m_bPressed)
                {
                    e.Graphics.DrawImage(
                        this.m_imgBackground, 
                        this.ClientRectangle, 0, 0,
                        this.m_imgBackground.Width,
                        this.m_imgBackground.Height, 
                        GraphicsUnit.Pixel, attr);
                }
                else
                {
                    e.Graphics.DrawImage(
                        this.m_imgBackground,
                        this.ClientRectangle, 0, 0,
                        this.m_imgBackground.Width,
                        this.m_imgBackground.Height, 
                        GraphicsUnit.Pixel, attr);
                }
            }
            base.OnPaint(e);
        }
    }
}
